package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.FactHouseSearchBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 实有房屋/出租房屋搜索
 */
public class FactHouseSearchActivity extends BaseActivity implements IPublicBeanView {

    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.rl_address)
    RelativeLayout rlAddress;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_nowLiveAddress)
    EditText etNowLiveAddress;
    @BindView(R.id.sp_houseUses)
    Spinner spHouseUses;
    @BindView(R.id.rbt_Yes)
    RadioButton rbtYes;
    @BindView(R.id.rbt_No)
    RadioButton rbtNo;
    @BindView(R.id.rg_isRentalHouse)
    RadioGroup rgIsRentalHouse;
    @BindView(R.id.rl_isRental)
    RelativeLayout rlIsRental;
    @BindView(R.id.et_houseAreaStart)
    EditText etHouseAreaStart;
    @BindView(R.id.et_houseAreaEnd)
    EditText etHouseAreaEnd;
    @BindView(R.id.rl_houseuse)
    RelativeLayout rlHouseuse;

    private PublicListPest publicListPest;
    private List<GlobalBean> houseUses;
    private boolean isRental;
    private Boolean isRentalHouse = null;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fact_house_search);
        ButterKnife.bind(this);
        setTitleName("实有房屋搜索");
        setTitleRight("查询");
        isRental = getIntent().getBooleanExtra("isRental", false);
        if (isRental) {
            rlIsRental.setVisibility(View.GONE);
        } else {
            rlIsRental.setVisibility(View.VISIBLE);
        }
        publicListPest = new PublicListPest(this);
        publicListPest.getPublicList("房屋用途");
        rgIsRentalHouse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.rbt_Yes) {
                    isRentalHouse = true;
                } else {
                    isRentalHouse = false;
                }
            }
        });
    }

    @OnClick({R.id.tv_right})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right:
                String address =  etAddress.getText().toString().trim();
                String name =  etName.getText().toString().trim();
                String NowLiveAddress =  etNowLiveAddress.getText().toString().trim();
                String HouseAreaStart =  etHouseAreaStart.getText().toString().trim();
                String HouseAreaEnd =  etHouseAreaEnd.getText().toString().trim();
                String userID = ((GlobalBean) spHouseUses.getSelectedItem()).getId();
                if (BaseUtil.isEmpty(address) && BaseUtil.isEmpty(name) && BaseUtil.isEmpty(NowLiveAddress)
                        && BaseUtil.isEmpty(HouseAreaStart) && BaseUtil.isEmpty(HouseAreaEnd)
                        && isRentalHouse == null && BaseUtil.isEmpty(userID) ){
                    ToastUtils.showToast(FactHouseSearchActivity.this,"请选择筛选条件");
                }else {
                    FactHouseSearchBean bean = new FactHouseSearchBean();
                    bean.address = address;
                    bean.name = name;
                    bean.nowLiveAddress = NowLiveAddress;
                    bean.houseAreaStart = HouseAreaStart;
                    bean.houseAreaEnd = HouseAreaEnd;
                    bean.houseUsesId = userID;
                    bean.isRentalHouse = isRentalHouse;
                    if (isRental) {
                        MyRxBus.get().post(new MsgEvent("RentHouseActivity", 2, bean));
                    } else {
                        MyRxBus.get().post(new MsgEvent("FactHouseActivity", 2, bean));
                    }
                    finish();
                }
                break;
        }
    }

    @Override
    public void showError(String message) {
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("房屋用途".equals(publicBean.getDomainName())) {
            houseUses = publicBean.getDicts();
            if (houseUses != null && houseUses.size() > 0) {
                GlobalBean globalBean = new GlobalBean();
                globalBean.setDisplayName("不限");
                globalBean.setId("");
                houseUses.add(0,globalBean);
                SpinnerPubAdapter adapter = new SpinnerPubAdapter(this, R.layout.item_text, houseUses);
                spHouseUses.setAdapter(adapter);
            } else {
                rlHouseuse.setVisibility(View.GONE);
            }
        }
    }
}
